package com.cjack.frenchexcel.service;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class SendMailService {

    private final static Logger LOG = LogManager.getLogger( SendMailService.class);
    @Value( "${email.sender}")
    private String sender;
//    @Autowired
    private JavaMailSender mailSender = new JavaMailSenderImpl();

    /**
     * 发送简单邮件
     * @param to 发送给谁
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo( to);
        message.setSubject( subject);
        message.setText( content);
        message.setFrom( sender);
        mailSender.send(message);
    }

    /**
     * 发送html邮件
     * @param to
     * @param subject
     * @param content
     */
    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper( message, true);
            helper.setTo( to);
            helper.setFrom( sender);
            helper.setSubject( subject);
            helper.setText( content, true);
            mailSender.send( message);
        } catch ( MessagingException e) {
            LOG.info( e.getMessage());
        }

    }

    /**
     * 发送附件邮件
     * @param to
     * @param subject
     * @param content
     * @param filePaths
     */
    public void sendAttachmentsMail(String to, String subject, String content, String[] filePaths) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper( message,true);
            helper.setTo( to);
            helper.setFrom( sender);
            helper.setSubject( subject);
            helper.setText( content, true);

            for( String filePath : filePaths){
                FileSystemResource file = new FileSystemResource( new File( filePath));
                String fileName = file.getFilename();
                //此处可以添加多个附件 zjy0910
                helper.addAttachment( fileName, file);
            }
            mailSender.send( message);
        } catch ( MessagingException e) {
            LOG.info( e.getMessage());
        }
    }

    /**
     * 发送图片邮件
     * @param to
     * @param subject
     * @param content
     * @param rscPath 图片路径
     * @param rscId
     */
    public void sendInlinResourceMail(String to, String subject, String content,
                                      String rscPath, String rscId) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo( to);
            helper.setFrom( sender);
            helper.setSubject( subject);
            helper.setText( content,true);

            //可以添加多个图片
            FileSystemResource res = new FileSystemResource( new File( rscPath));
            helper.addInline( rscId, res);
            mailSender.send (message);
        } catch (MessagingException e) {
            LOG.info( e.getMessage());
        }
    }
}
